//
//  EFShoppingCardManager.m
//  EnjogFood
//
//  Created by 张文瀚 on 2017/5/21.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFShoppingCartManager.h"
#import "EFSepecialItem.h"

@implementation EFShoppingCartManager
- (instancetype)init{
    self = [super init];
    if (self) {
        self.scanProductsCart = [NSMutableArray arrayWithCapacity:10];
        self.storeProductsCart = [NSMutableArray arrayWithCapacity:10];
    }
    return self;
}

- (NSInteger)scanCount{
    return self.scanProductsCart.count;
}

- (NSInteger)storeCount{
    return self.storeProductsCart.count;
}

- (CGFloat)scanTotalPrice{
    CGFloat totalPrice = 0;
    for (EFSepecialItem *item in self.scanProductsCart) {
        NSInteger count = item.qty.integerValue;
        CGFloat price = item.price.floatValue;
        totalPrice += count * price;
    }
    return totalPrice;
}

- (CGFloat)storeTotalPrice{
    CGFloat totalPrice = 0;
    for (EFSepecialItem *item in self.storeProductsCart) {
        NSInteger count = item.qty.integerValue;
        CGFloat price = item.price.floatValue;
        totalPrice += count * price;
    }
    return totalPrice;
}

- (void)uniqueScanProduct{
     NSMutableArray *uniqueArray = [NSMutableArray arrayWithCapacity:10];
    for (NSInteger i = 0; i < self.scanProductsCart.count; i++) {
        EFSepecialItem *itemOne = [self.scanProductsCart objectAtIndex:i];
        Boolean bCounted = false;
        for (NSInteger j = 0; j < uniqueArray.count; j ++) {
            EFSepecialItem *uItem = [uniqueArray objectAtIndex:j];
            if (itemOne.id.integerValue == uItem.id.integerValue) {
                bCounted = true;
            }
        }
        if(bCounted)
            continue;
        
        for (NSInteger j = i + 1; j < self.scanProductsCart.count; j++){
            EFSepecialItem *itemTwo = [self.scanProductsCart objectAtIndex:j];
            if(itemOne.id.integerValue == itemTwo.id.intValue){
                itemOne.qty =  [NSNumber numberWithInteger:itemOne.qty.integerValue + itemTwo.qty.integerValue];
            }
        }
        [uniqueArray addObject:itemOne];
    }
    
    self.scanProductsCart = uniqueArray;
    
//    NSMutableArray *uniqueArray = [NSMutableArray arrayWithCapacity:10];
//    for (NSInteger index = 0; index < self.scanProductsCart.count; index++) {
//        EFSepecialItem *item = [self.scanProductsCart objectAtIndex:index];
//        if (index == 0) {
//            [uniqueArray addObject:item];
//        }else{
//            for (NSInteger j = 0; j < uniqueArray.count; j ++) {
//                EFSepecialItem *uItem = [uniqueArray objectAtIndex:j];
//                if (item.productId.integerValue == uItem.productId.integerValue) {
//                    uItem.qty =  [NSNumber numberWithInteger:uItem.qty.integerValue + 1];
//                }else{
//                    [uniqueArray addObject:item];
//                }
//            }
//        }
//    }
//    self.scanProductsCart = uniqueArray;
}

- (void)uniqueStoreProduct{
    NSMutableArray *uniqueArray = [NSMutableArray arrayWithCapacity:10];
//    for (NSInteger index = 0; index < self.storeProductsCart.count; index++) {
//        EFSepecialItem *item = [self.storeProductsCart objectAtIndex:index];
//        if (index == 0) {
//            [uniqueArray addObject:item];
//        }else{
//            for (NSInteger j = 0; j < uniqueArray.count; j ++) {
//                EFSepecialItem *uItem = [uniqueArray objectAtIndex:j];
//                if (item.productId.integerValue == uItem.productId.integerValue) {
//                    uItem.qty =  [NSNumber numberWithInteger:uItem.qty.integerValue + 1];
//                }else{
//                    [uniqueArray addObject:item];
//                }
//            }
//        }
//        self.storeProductsCart = uniqueArray;
//    }
    for (NSInteger i = 0; i < self.storeProductsCart.count; i++) {
        EFSepecialItem *itemOne = [self.storeProductsCart objectAtIndex:i];
        Boolean bCounted = false;
        for (NSInteger j = 0; j < uniqueArray.count; j ++) {
            EFSepecialItem *uItem = [uniqueArray objectAtIndex:j];
            if (itemOne.id.integerValue == uItem.id.integerValue) {
                bCounted = true;
            }
        }
        if(bCounted)
            continue;
        
        for (NSInteger j = i + 1; j < self.storeProductsCart.count; j++){
            EFSepecialItem *itemTwo = [self.storeProductsCart objectAtIndex:j];
            if(itemOne.id.integerValue == itemTwo.id.intValue){
                itemOne.qty =  [NSNumber numberWithInteger:itemOne.qty.integerValue + itemTwo.qty.integerValue];
            }
        }
        [uniqueArray addObject:itemOne];
    }
    
    self.storeProductsCart = uniqueArray;
    
}

- (void)addToScanProduct:(EFSepecialItem *)item{
    [self.scanProductsCart addObject:item];
    [self uniqueScanProduct];
}

- (void)addToStoreProduct:(EFSepecialItem *)item{
    [self.storeProductsCart addObject:item];
    [self uniqueStoreProduct];
}

- (void)addScanProduct:(EFSepecialItem *)item{
    for (EFSepecialItem *i in self.scanProductsCart) {
        if (item.id.integerValue == i.id.integerValue) {
           i.qty = [NSNumber numberWithInteger:(i.qty.integerValue + 1)];
        }
    }
}

- (void)addStorProduct:(EFSepecialItem *)item{
    for (EFSepecialItem *i in self.storeProductsCart) {
        if (item.id.integerValue == i.id.integerValue) {
            i.qty = [NSNumber numberWithInteger:(i.qty.integerValue + 1)];
        }
    }
}


- (void)subScanProduct:(EFSepecialItem *)item{
    for (EFSepecialItem *i in self.scanProductsCart) {
        if (item.id.integerValue == i.id.integerValue) {
            if (i.qty.integerValue - 1 == 0) {
                [self.scanProductsCart removeObject:i];
            }else{
                i.qty = [NSNumber numberWithInteger:(i.qty.integerValue - 1)];
            }
        }
    }
}

- (void)subStorProduct:(EFSepecialItem *)item{
    for (EFSepecialItem *i in self.storeProductsCart) {
        if (item.id.integerValue == i.id.integerValue) {
            if (i.qty.integerValue - 1 == 0) {
                [self.storeProductsCart removeObject:i];
            }else{
                i.qty = [NSNumber numberWithInteger:(i.qty.integerValue - 1)];
            }
        }
    }
}

-(void)removeObject:(NSString *)productId type:(NSInteger)type{//1 快速扫描  0 商城
    if(type == 0){
        int index = -1;
        for (int i = 0 ; i < self.storeProductsCart.count ; ++i) {
            EFSepecialItem *item =[self.storeProductsCart objectAtIndex:i];
            if([item.id isEqual:productId]){
                index =  i;
                break;
            }
        }
        if(index != -1){
            [self.storeProductsCart removeObjectAtIndex:index];
        }
    }else{
        int index = -1;
        for (int i = 0 ; i < self.scanProductsCart.count ; ++i) {
            EFSepecialItem *item =[self.scanProductsCart objectAtIndex:i];
            if([item.id isEqual:productId]){
                index =  i;
                break;
            }
        }
        if(index != -1){
            [self.scanProductsCart removeObjectAtIndex:index];
        }
    }
}

@end
